package com.mowh.multithreading.thread.per.message;

/**
 * @author mowh1gd
 * @create 2023/11/22 14:39
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("main BEGIN");
        Host host = new Host();//(1) java.lang.Thread
        host.request(10, 'A');
        host.request(20, 'B');
        host.request(30, 'C');
        System.out.println("main END");


//        RunnableHost host = new RunnableHost();//(2) java.lang.Runnable

//        ThreadFactoryHost host = new ThreadFactoryHost(//(3) java.util.concurrent.ThreadFactory（工厂方法模式）
//                new ThreadFactory() {//创建工厂方法(3.1): new一个java.util.concurrent.ThreadFactory
//                    @Override
//                    public Thread newThread(Runnable r) {//工厂方法模式：把线程创建与工作内容(用Runnable实例，关注工作内容本身)分离
//                        return new Thread(r);//线程创建
//                    }
//                }
//        );

//        ThreadFactoryHost host = new ThreadFactoryHost(
//                Executors.defaultThreadFactory()//创建工厂方法(3.2): 通过java.util.concurrent.Executors的静态方法获取默认ThreadFactory
//        );

//        ExecutorHost host = new ExecutorHost(//(4) java.util.concurrent.Executor
//                new Executor() {
//                    @Override
//                    public void execute(Runnable r) {
//                        new Thread(r).start();//(4.1) 手动创建线程
//                    }
//                }
//        );

//        ExecutorService executorService = Executors.newCachedThreadPool();
//        ExecutorHost host = new ExecutorHost(
//                executorService//(4.2) 不创建新线程，而是复用空闲的线程
//        );
//        try {
//            host.request(10, 'A');
//            host.request(20, 'B');
//            host.request(30, 'C');
//        } finally {
//            executorService.shutdown();
//            System.out.println("main END");
//        }

//        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
//        ScheduledExecutorServiceHost host = new ScheduledExecutorServiceHost(//(5) java.util.concurrent.ScheduledExecutorService
//                scheduledExecutorService//不创建新线程，而是复用空闲的线程，供调度运行
//        );
//        try {
//            host.request(10, 'A');
//            host.request(20, 'B');
//            host.request(30, 'C');
//        } finally {
//            scheduledExecutorService.shutdown();
//            System.out.println("main END");
//        }

    }
}
